package br.com.lazyprogrammer.worldsoccermanager.network.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import br.com.lazyprogrammer.worldsoccermanager.network.JSONConstants;
import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.URLParams;
import br.com.lazyprogrammer.worldsoccermanager.network.builders.URLBuilder;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.PlayerListener;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Player;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.Parser;

public class GetsPlayerTask extends AsyncTask<Integer, Void, Request.Response> {
	
	private Context context;
	private Request.Callback callback;
	private PlayerListener listener;
	
	public GetsPlayerTask(Context context, Request.Callback callback, PlayerListener listener) {
		this.context	= context;
		this.callback	= callback;
		this.listener	= listener;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		callback.onStart(getClass());
	}

	@Override
	protected Request.Response doInBackground(Integer... param) {
		// TODO Auto-generated method stub
		int page	= param[0];
		int limit	= param[1];
		Parser parser	= new Parser();
		Request.Response status;
		
		try {
			JSONObject parent	= new Request().post(new URLBuilder().buildUrlGame(URLBuilder.Type.Player), new URLParams().buildGetsPlayerParams(page, limit));
			status	= Request.Response.values()[parser.parseResponse(parent)];
			if (status.equals(Request.Response.Success)) {
                JSONArray array = parent.getJSONArray(JSONConstants.LIST);
                List<Player> players = new ArrayList<Player>();
                for (int i = 0; i < array.length(); i++)
                    players.add(parser.parsePlayer(array.getJSONObject(i).optJSONObject(JSONConstants.PLAYER)));
                listener.onPlayersLoaded(players);
            }
        } catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.SocketTimeout;
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.UnsupportedEncoding;
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.ClientProtocol;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IllegalState;
			e.printStackTrace();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.NullPointer;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IO;
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.JSON;
			e.printStackTrace();
		}
		return status;
	}
	
	@Override
	protected void onPostExecute(Request.Response result) {
		if (isCancelled())
			callback.onCancel(getClass());
		else {
			if (result.equals(Request.Response.Success))
				callback.onSuccess(getClass(), result);
			else
				callback.onError(getClass(), result, Message.getException(context, result));
		}
	}

}
